{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">货&emsp;&emsp;号：</label>
                        <div class="layui-input-inline">
                            <input name="goods_no" value="" class="layui-input" type="text" placeholder="输入货号"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">商&nbsp;&nbsp;品&nbsp;&nbsp;名：</label>
                        <div class="layui-input-inline">
                            <input name="goods_name" value="" class="layui-input" type="text" placeholder="输入商品名"/>
                        </div>
                    </div>

                    <div class="layui-inline ">
                        <label class="layui-form-label">状&emsp;&emsp;态：</label>
                        <div class="layui-input-inline">
                            <select name="status">
                                <option value="" selected>所有</option>
                                <option value="1" >正常</option>
                                <option value="2" >下架</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline" style="padding-right: 110px;">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchUser" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>

                            <button id="btnAddAuth" class="layui-btn icon-btn ">

                                <i class="layui-icon">&#xe654;</i>发布商品
                            </button>
                        <!--<button id="btnAddUser" class="layui-btn icon-btn {:auth('mall.store/add')}" ><i class="layui-icon">&#xe654;</i>添加</button>-->
                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options" >
    <div class="layui-btn-group">

        {{#  if({$type} !=1){ }}
        <a class="layui-btn layui-btn-sm {:auth('market.seckill/edit')}" lay-event="edit" >编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-sm {:auth('market.seckill/delete')}" lay-event="del">删除</a>

        {{#  } else { }}
        <a class="layui-btn layui-btn-sm {:auth('market.seckill/check')}" lay-event="check" >预览</a>

        {{#  } }}


    </div>
</script>
<!-- 表格状态列 -->
<script type="text/html" id="store">
    {{#  if(d.store){ }}
    {{ d.store.name }}
    {{#  } else { }}
    商家已删除
    {{#  } }}
</script>
<script type="text/html" id="tableStateUser">
    <input type="checkbox" lay-filter="ckStateUser" value="{d.id}" lay-skin="switch"
           lay-text="通过|驳回" {d.status==1?'checked':''}/>
</script>
<script type="text/html" id="tableStateUser1">
    {{#  if(d.status == 0 || d.status == 3){ }}
    <span class="layui-badge layui-bg-cyan">待平台审核</span>


    {{#  } else { }}
    <input type="checkbox" lay-filter="ckStateUser1"  value="{{d.id}}" lay-skin="switch"
           lay-text="上架|下架" {{d.status==1?'checked':''}}/>


    {{#  } }}
</script>

<div class="layui-card-body">
    <script type="text/html" id="thumb">

        {{# if(d.thumb){ }}
        <a href="{{d.thumb}}" target="_blank" title="点击查看">
            <img src="{{d.thumb}}" alt="" width="28" height="28">
        </a>
        {{#  } }}
    </script>
</div>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        let ins = "{$type}"
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:__url("market.seckill/getData")}',
            where : {
                'type':ins
            },
            page: true,
            toolbar: true,
            cellMinWidth: 100,
            height:700,
            cols: [[ //表头
                {field: 'id',title: 'ID', width: 50}
                , {field: 'market_title', title: '标题', width: 120}
                , {field: 'store', title: '商家名', toolbar: '#store'}

                , {field: 'thumb', title: '主图', toolbar: '#thumb', width: 100}


                , {field: 'market_price', title: '活动单价'},

                {field: 'inventory', title: '库存'},
                {
                    title: '状态', templet: function (d) {
                    var strs = ['<span class="layui-badge layui-bg-orange">待审核</span>',
                        '<span class="layui-badge layui-bg-green">已上架</span>',
                        '<span class="layui-badge layui-bg-red">已下架</span>',
                        '<span class="layui-badge layui-bg-red">已驳回</span>'];
                    return strs[d.status];
                }, align: 'center', width: 100
                }
                , {field: 'create_time', title: '创建时间',width:160}
                //,{field: 'is_top', sort: true,width:100, templet: '#tableStateUser1', title: '上下架操作'}
                ,{field: 'status', sort: true,width:120, templet: '#tableStateUser1', title: '上下架操作'}
                , {fixed: 'right',field: 'id', width: 140, align: 'center', title:'操作', toolbar: '#options'}
                /*, {fixed: 'right', width: 140, align: 'center', toolbar: '#options'}*/
            ]]
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        // 添加按钮点击事件
        $('#btnAddAuth').click(function () {
            showEditModel('','{:__url("market.seckill/add")}');
        }) ;
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data.id,'/admin/market.seckill/edit/id/'+data.id);
                //location.href = '/admin/market.seckill/edit/id/'+data.id;
            }else if (layEvent === 'check') {
                showEditModel(data.id,'/admin/market.seckill/check/id/'+data.id);
                //location.href = '/admin/mall.item/edit/id/'+data.id;
            }
            else if (layEvent === 'del') { // 删除
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:url('market.seckill/destroy')}", {
                        ids: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            }
        });

        // 显示表单弹窗
        function showEditModel(mAuth,url) {
            console.log(mAuth);
            layer.full(admin.open({
                type: 2,
                title: (mAuth ? '修改' : '添加') + '商品',
                content:url,

                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');

                    // 表单提交事件
                    form.on('submit(modelSubmitAuth)', function (data) {
                        console.log(data);return;
                        if(data.field.id == '') {
                            data.field.id = '0';
                        }
                        console.log(data.field);
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            }));
        }

        //审核
        //
        form.on('switch(ckStateUser1)', function (obj) {
            layer.load(2);
            $.post('{:__url("market.seckill/modify")}', {
                id: obj.elem.value,
                field:'status',
                value: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        //置顶
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post('{:__url("market.seckill/modify")}', {
                id: obj.elem.value,
                is_top: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

    });
</script>

</body>
</html>
